﻿using DevComponents.DotNetBar;
using Kimd.Logic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace WorkStation.UI.Controls
{
    public partial class ModuleList : UserControl
    {
        private object lockObj = new object();
        private List<string> _moduleNames = new List<string>() { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
        private List<string> _warnModuleList;
        private List<ButtonX> _moduleButtons;
        private List<ButtonX> _moduleCircleButtons;
        private List<Point> _originLocations;
        private int _originWidth;
        public int OriginWidth
        {
            get { return _originWidth; }
            set { _originWidth = value; }
        }
        private int _originHeight;
        public int OriginHeight
        {
            get { return _originHeight; }
            set { _originHeight = value; }
        }
        /// <summary>
        /// 是否处于可设置的权限
        /// </summary>
        public bool IsInEditUserRole { get; set; }

        public event EventHandler OnModuleSelected;//自定义的事件
        public List<string> ModuleNames 
        { 
            get
            {
                return _moduleNames;
            }
            set
            {
                _moduleNames = value;
                createButton();
                if(_moduleNames != null)
                    createCircleButton(_moduleNames.Count);
            }
        }
        /// <summary>
        /// circle color:
        /// #ea986c
        /// #eeb173
        /// #f3ca7e
        /// #f9f28b
        /// #c8db8c
        /// #aad08f
        /// #87c38f
        /// #83c6c2
        /// #7dc5eb
        /// #87a7d6
        /// #8992c8
        /// #a686ba
        /// #bd8cbb
        /// #be8dbd
        /// #e89abe
        /// #e8989a
        /// </summary>
        public ModuleList()
        {
            InitializeComponent();
            _warnModuleList = new List<string>();
            _moduleButtons = new List<ButtonX>();
            _moduleCircleButtons = new List<ButtonX>();
            _originLocations = new List<Point>();
        }

        private void ModuleList_Load(object sender, EventArgs e)
        {
        }
        public void createCircleButton(int moduleCount)
        {
            _moduleCircleButtons.Clear();
            _originLocations.Clear();
            byte startChar = 0x41;
            for (int i = 0; i < moduleCount; i++)
            {
                ButtonX buttonX12 = new ButtonX();
                buttonX12 = new DevComponents.DotNetBar.ButtonX();
                buttonX12.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton;
                buttonX12.FocusCuesEnabled = false;
                buttonX12.Font = new System.Drawing.Font("Arial Black", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                buttonX12.Name = "buttonX12";
                buttonX12.Shape = new DevComponents.DotNetBar.EllipticalShapeDescriptor();
                buttonX12.Size = new System.Drawing.Size(55, 53);
                buttonX12.Style = eDotNetBarStyle.StyleManagerControlled;
                buttonX12.TabIndex = 15;
                buttonX12.Text = Encoding.ASCII.GetString(new byte[] {startChar});
                buttonX12.TextColor = System.Drawing.Color.White;
                buttonX12.ColorTable = eButtonColor.BlueWithBackground;
                if(ModuleNames.Count > i)
                    buttonX12.Tag = $"{Encoding.ASCII.GetString(new byte[] { startChar })}-{ModuleNames[i]}";
                buttonX12.Click += Module_Click2;
                startChar += 0x01;
                _moduleCircleButtons.Add(buttonX12);

                if (i == 0)
                    _originLocations.Add(new Point(160, 320));
                else if (i == 1)
                    _originLocations.Add(new Point(615, 320));
                else if (i == 2)
                    _originLocations.Add(new Point(615, 80));
                else if (i == 3)
                    _originLocations.Add(new Point(160, 80));
                else if (i == 4)
                    _originLocations.Add(new Point(387, 380));
                else if (i == 5)
                    _originLocations.Add(new Point(387, 20));
                else if (i == 6)
                    _originLocations.Add(new Point(760, 0));
                else if (i == 7)
                    _originLocations.Add(new Point(190, 240));
                //else if (i == 7)
                //    _originLocations.Add(new Point(457, 24));
                else if (i == 8)
                    _originLocations.Add(new Point(585, 160));
                else if (i == 9)
                    _originLocations.Add(new Point(700, 14));
                else if (i == 10)
                    _originLocations.Add(new Point(198, 500));
            }
        }
        public List<ButtonX> GetCircleButtons()
        {
            return _moduleCircleButtons;
        }
        public List<Point> GetLocations()
        {
            return _originLocations;
        }
        private void createButton()
        {
            this.panelEx1.Controls.Clear();
            byte startChar = 0x41;
            if (ModuleNames == null)
                return;
            for (int i = 0; i < ModuleNames.Count; i++)
            {
                ButtonX buttonX1 = new ButtonX();
                buttonX1 = new DevComponents.DotNetBar.ButtonX();
                //buttonX1 = new Button();
                // 
                // buttonX1
                // 
                buttonX1.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton;
                buttonX1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
                buttonX1.BackColor = System.Drawing.SystemColors.Control;
                //buttonX1.ColorTable = DevComponents.DotNetBar.eButtonColor.Blue;
                buttonX1.Font = new System.Drawing.Font("微软雅黑", 9.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                buttonX1.Location = new System.Drawing.Point(5, 10 + 29 * i);
                buttonX1.Name = $"Module{i}";
                buttonX1.PopupSide = DevComponents.DotNetBar.ePopupSide.Right;
                buttonX1.Shape = new DevComponents.DotNetBar.RoundRectangleShapeDescriptor(4);
                buttonX1.Size = new System.Drawing.Size(this.Width - 2 * 5, 26);
                //buttonX1.Style = DevComponents.DotNetBar.eDotNetBarStyle.OfficeMobile2014;
                buttonX1.TabIndex = 0;
                buttonX1.Text = $"{Encoding.ASCII.GetString(new byte[] { startChar })}-{ModuleNames[i]}";
                startChar += 0x01;
                buttonX1.Click += Module_Click;
                buttonX1.Image = iL_Circles.Images[i];
                buttonX1.Style = eDotNetBarStyle.StyleManagerControlled;
                buttonX1.ColorTable = eButtonColor.Flat;
                //if (!string.IsNullOrEmpty(ModuleNames[i]))
                //{
                //    if (SystemConfig.Instance.ModuleName2Disable.ContainsKey(ModuleNames[i]))
                //    {
                //        buttonX1.Checked = !SystemConfig.Instance.ModuleName2Disable[ModuleNames[i]];
                //        buttonX1.BackColor = SystemConfig.Instance.ModuleName2Disable[ModuleNames[i]] ? Color.Gray : SystemColors.Control;
                //        buttonX1.TextColor = SystemConfig.Instance.ModuleName2Disable[ModuleNames[i]] ? Color.White : Color.Black;
                //    }
                //}
                buttonX1.Checked = true;
                buttonX1.BackColor = SystemColors.Control;
                buttonX1.TextColor = Color.Black;
                this.panelEx1.Controls.Add(buttonX1);
            }
        }

        private void Module_Click(object sender, EventArgs e)
        {
            string moduleName = (sender as ButtonX).Text.Split('-')[1];
            ModuleSelectArgs ee = new ModuleSelectArgs() { ModuleName = moduleName };
            if(IsInEditUserRole)
            {
                if (SystemConfig.Instance.ModuleName2Disable.ContainsKey(moduleName))
                {
                    string msg = SystemConfig.Instance.ModuleName2Disable[moduleName] ? "启用" : "不启用";
                    if (MessageBox.Show($"确定将该模组设置为 [{msg}] 状态吗?", "模组启用", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        SystemConfig.Instance.ModuleName2Disable[moduleName] = !SystemConfig.Instance.ModuleName2Disable[moduleName];
                        FlowManager.Instance.Flows[moduleName].Status.IsEnabled = !SystemConfig.Instance.ModuleName2Disable[moduleName];
                        (sender as ButtonX).Checked = !SystemConfig.Instance.ModuleName2Disable[moduleName];
                        (sender as ButtonX).BackColor = SystemConfig.Instance.ModuleName2Disable[moduleName] ? Color.Gray : SystemColors.Control;
                        (sender as ButtonX).TextColor = SystemConfig.Instance.ModuleName2Disable[moduleName] ? Color.White : Color.Black;
                        SystemConfig.Instance.Save();
                    }
                }
            }
            else
            {
                SystemInfo.ShowToastInfo(this.Parent, "当前权限无法设置模组启用状态", true);
            }
            //if (OnModuleSelected != null)
            //    OnModuleSelected(this, ee);
        }
        private void Module_Click2(object sender, EventArgs e)
        {
            ModuleSelectArgs ee = new ModuleSelectArgs() { ModuleName = (sender as ButtonX).Tag.ToString() };
            if (OnModuleSelected != null)
                OnModuleSelected(this, ee);
        }
        public void ModuleWarn()
        {
            if (_warnModuleList.Count == 0)
                return;
            foreach (var item in this.panelEx1.Controls)
            {
                if (_warnModuleList.Contains((item as ButtonX).Text.Split('-')[1]))
                {
                    (item as ButtonX).BackColor = Color.Red;
                    (item as ButtonX).ForeColor = Color.Red;
                    foreach (ButtonX buttonX in _moduleCircleButtons)
                    {
                        if(buttonX.Text.Equals((item as ButtonX).Text.Split('-')[0]))
                        {
                            buttonX.BackColor = Color.Red;
                            buttonX.ForeColor = Color.Red;
                            break;
                        }    
                    }
                    break;
                }
            }
        }
        public void ResetWarn()
        {
            if (_warnModuleList.Count == 0)
                return;
            foreach (var item in this.panelEx1.Controls)
            {
                if (string.IsNullOrEmpty((item as ButtonX).Text))
                    continue;
                if (_warnModuleList.Contains((item as ButtonX).Text.Split('-')[1]))
                {
                    (item as ButtonX).BackColor = System.Drawing.SystemColors.Control;
                    (item as ButtonX).ForeColor = Color.Black;
                    foreach (ButtonX buttonX in _moduleCircleButtons)
                    {
                        if (buttonX.Text.Equals((item as ButtonX).Text.Split('-')[0]))
                        {
                            buttonX.BackColor = System.Drawing.SystemColors.Control;
                            buttonX.ForeColor = Color.Black;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        public void AddWarnModule(string moduleName)
        {
            lock(lockObj)
            {
                _warnModuleList.Add(moduleName);
            }
        }
        public void RemoveWarnModule(string moduleName = "")
        {
            lock (lockObj)
            {
                ResetWarn();
                if (!string.IsNullOrEmpty(moduleName))
                    _warnModuleList.Remove(moduleName);
                else
                    _warnModuleList.Clear();
            }
        }
        bool isRed = false;
        private void t_Module_Tick(object sender, EventArgs e)
        {
            isRed = !isRed;
            if(isRed)
            {
                ModuleWarn();
            }
            else
            {
                ResetWarn();
            }
        }
        public void RelocateCircleButtons(int originW, int originH, int currentW, int currentH)
        {
            for (int i = 0; i < _moduleCircleButtons.Count; i++)
            {
                if(i < _originLocations.Count)
                    _moduleCircleButtons[i].Location = new Point((int)(_originLocations[i].X * 1.0 / originW * currentW), (int)(_originLocations[i].Y * 1.0 / originH * currentH));
            }
        }
    }
    public class ModuleSelectArgs : EventArgs
    {
        public string ModuleName { get; set; }
    }
}
